package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Author:zhangyang
 * @Date: 16:00
 * @Version 1.0
 */
@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        //处理post乱码
        request.setCharacterEncoding("utf-8");
        //1)获取参数
        //获取全部参数
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        //beanUtils工具类
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(user);

        //2)调用service
        UserService userService = new UserServiceImpl() ;
        userService.addUser(user) ;

        //3)重定向到findUserList
//        response.sendRedirect(request.getContextPath()+"/findUserList");
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
